package com.system.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.system.dao.DaoFactory;
import com.system.dao.UserDao;
import com.system.model.User;
import com.system.service.LoginService;
import com.system.util.ReflectUtil;

import net.sf.json.JSONObject;

public class LoginServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public LoginServlet(){super();}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("gbk");
		response.setHeader("content-type", "text/html; charset=utf-8");
		response.setHeader("Access-Control-Allow-Origin", "*"); 
		System.out.println("receving resquest"+request.getParameter("username")
		+";"+request.getParameter("password"));
		ReflectUtil.invokeMethods(this.getClass(), request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	/**
	 * 管理员登录
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void adminLogin(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		LoginService service=new LoginService();
		JSONObject json=service.checkoutAdminLogin(username, password);
		response.getWriter().write(json.toString());
//		this.addTest();
		
	}
	/**
	 * 教师登录
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void teacherLogin(HttpServletRequest request, HttpServletResponse response)throws Exception{
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		LoginService service=new LoginService();
		JSONObject json=service.checkoutTeaLogin(username, password);
		response.getWriter().write(json.toString());
	}
	/**
	 * 学生登录
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void studentLogin(HttpServletRequest request, HttpServletResponse response)throws Exception{
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		LoginService service=new LoginService();
		JSONObject json=service.checkoutStuLogin(username, password);
		response.getWriter().write(json.toString());
	}
	
	public void changePsw(HttpServletRequest request, HttpServletResponse response)throws Exception{
		String username=request.getParameter("username");
		String oldpsw=request.getParameter("oldpsw");
		String newpsw=request.getParameter("newpsw");
		String post=request.getParameter("post");
		LoginService service=new LoginService();
		JSONObject json=service.checkoutChangePsd(newpsw, oldpsw, username, post);
		response.getWriter().write(json.toString());
	}
	
	//测试添加功能
	public void addTest() {
		User u = new User();
		u.setUsername("abc");
		u.setPassword("123");
		u.setPost("admin");
		DaoFactory factory = DaoFactory.getInstance();
		UserDao userDao = factory.createUserDao();
		userDao.add(u);
		System.out.println("hehe");
	}
}
